package cn.wolfcode.wechat.web.controller;

import cn.wolfcode.wechat.domain.ArticlesItem;
import cn.wolfcode.wechat.domain.InMessageEntity;
import cn.wolfcode.wechat.domain.OutMessageEntity;
import cn.wolfcode.wechat.util.DecriptUtil;
import cn.wolfcode.wechat.util.JaxbUtil;
import cn.wolfcode.wechat.util.TokenUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.JAXBException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Date;

@RestController
public class WeChatController {
    @RequestMapping(value = "/wechat",method = RequestMethod.GET)
    public String weChat(String signature,String timestamp,String nonce,String echostr){
//        1）将token、timestamp、nonce三个参数进行字典序排序
        String[] arr = {TokenUtil.TOKEN, timestamp, nonce};
        Arrays.sort(arr);
//        2）将三个参数字符串拼接成一个字符串进行sha1加密
        StringBuilder sb = new StringBuilder();
        for (String s : arr) {
            sb.append(s);
        }
        String str = DecriptUtil.SHA1(sb.toString());
//        3）开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
        if (str.equals(signature)){
            System.out.println("接入成功");
            return echostr;
        }
        System.out.println("接入失败");
        return null;
    }
    @RequestMapping(value = "/wechat",method = RequestMethod.POST)
    public Object inMessageEntity(@RequestBody InMessageEntity inMessageEntity) throws JAXBException {
        OutMessageEntity outMessageEntity = new OutMessageEntity();
        outMessageEntity.setToUserName(inMessageEntity.getFromUserName());
        outMessageEntity.setFromUserName(inMessageEntity.getToUserName());
        outMessageEntity.setCreateTime(new Date().getTime());
        outMessageEntity.setMsgType(inMessageEntity.getMsgType());
        if ("text".equals(inMessageEntity.getMsgType())){
            String content = inMessageEntity.getContent();;
            String outContent = null;
            if (content.contains("开班")){
                outContent = "上海Java基础班第05期于2018/05/10开班\n" + "广州Java基础班第24期于2018/04/02开班";
            }else if (content.contains("地址")){
                outContent = "北京校区：北京昌平区沙河镇万家灯火装饰城2楼8077号\n" +
                        "广州校区：广州市天河区棠下涌东路大地工业区D栋六楼\n" +
                        "上海校区：上海市青浦区华新镇华隆路1777号E通世界商务园华新园A座4楼402";
            }else if (content.contains("你好")) {
                outContent = "你也好啊!";
            }else {
                outContent = inMessageEntity.getContent();
            }
            outMessageEntity.setContent(outContent);
        }else if ("image".equals(inMessageEntity.getMsgType())){
            outMessageEntity.setMediaId(new String[]{inMessageEntity.getMediaId()});
        }else if ("event".equals(inMessageEntity.getMsgType())){
            //图文事件(订阅)
            if ("subscribe".equals(inMessageEntity.getEvent())){
                //回复文本
                /*outMessageEntity.setContent("欢迎关注[微笑]");
                outMessageEntity.setMsgType("text");*/

                //回复图文
                outMessageEntity.setMsgType("news");
                outMessageEntity.setArticleCount(1);
                ArticlesItem articlesItem = new ArticlesItem();
                articlesItem.setTitle("【Java201228期】平均薪资9953元，最高薪资15000元！");//图文消息标题
                articlesItem.setDescription("■ 如上图所示，该班毕业34个工作日，班级就业率达到90%，平均薪资9953元，最高薪资15000元！\n" +
                        "\n" +
                        " \n" +
                        "\n" +
                        "■ 目前就业工作仍在持续进行中，尚未完成就业的同学将在老师的指导下继续求职、面试。\n" +
                        "\n" +
                        "\n" +
                        "■ 叩丁狼是业内少有的就业数据可溯源的机构，每一个出去就业的同学，就业老师都会如实记录就业数据，以供同学们查询。");
                articlesItem.setPicUrl("http://www.wolfcode.cn/upload/images/articleImg/20211028/7a80fc3b-ad9d-4d67-a698-8ec3d7127835.png");////图片链接，支持JPG、PNG格式，较好的效果为大图360*200，小图200*200
                articlesItem.setUrl("http://www.wolfcode.cn/new/portal/articlePage.html?id=8286");//点击图文消息跳转链接
                outMessageEntity.setItem(new ArticlesItem[]{articlesItem});
            }
        }
        String xml = JaxbUtil.beanToXml(outMessageEntity, OutMessageEntity.class, "UTF-8");
        return xml;
    }
}
